/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.canvaschart.client.xy;

import java.util.Iterator;
import java.util.LinkedList;

import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Widget;

import net.kornr.abstractcanvas.client.ICanvasExt;
import net.kornr.canvaschart.client.core.AnnotatedCanvas;
import net.kornr.canvaschart.client.core.Annotation;
import net.kornr.canvaschart.client.core.AnnotationSupport;
import net.kornr.canvaschart.client.core.Background;
import net.kornr.canvaschart.client.core.CoordinateSystem;
import net.kornr.canvaschart.client.core.Point;

public class DefaultXYPlotter implements XYPlotter, MouseListener 
{
	private CoordinateSystem m_coordinates = new CoordinateSystem();
	private LinkedList<Background> m_backgrounds = new LinkedList<Background>();
	protected LinkedList<XYRenderer> m_rendererseq = new LinkedList<XYRenderer>();
	protected ICanvasExt m_canvas;
	
	public DefaultXYPlotter(ICanvasExt canvas)
	{
		m_canvas = canvas;
		init();
	}
	
	public void addBackground(Background bg)
	{
		m_backgrounds.add(bg);
	}

	public void add(XYRenderer renderer)
	{
		m_rendererseq.add(renderer);
	}

	public CoordinateSystem getCoordinateSystem()
	{
		return m_coordinates;
	}
	
	public void init()
	{
		m_coordinates.addVerticalBorder();

		if (!(m_canvas instanceof AnnotatedCanvas))
			return;

		AnnotatedCanvas anncan = (AnnotatedCanvas)m_canvas;
		anncan.addMouseListener(this);
	}
	
	public void plot() 
	{
		m_canvas.clear();
		
		for (Iterator<Background> i=m_backgrounds.iterator(); i.hasNext(); )
		{
			Background bg = i.next();
			bg.drawBackground(m_canvas, m_coordinates);
		}

		for(Iterator<XYRenderer> i = m_rendererseq.iterator(); i.hasNext(); )
		{
			XYRenderer rend = i.next();
			rend.render(m_canvas, m_coordinates);
		}
	}

	public void onMouseDown(Widget sender, int x, int y) {
	}
	public void onMouseEnter(Widget sender) {
	}
	public void onMouseLeave(Widget sender) {
	}

	public void onMouseMove(Widget sender, int x, int y) 
	{
		if (!(m_canvas instanceof AnnotatedCanvas))
			return;
		
		AnnotatedCanvas anncan = (AnnotatedCanvas)m_canvas;
		Annotation anno = anncan.getAnnotation();
		
		Point datacoord = m_coordinates.getDataCoord(x, y);
		Point pixelcoord = new Point(x,y);
		
		for(Iterator<XYRenderer> i = m_rendererseq.iterator(); i.hasNext(); )
		{
			XYRenderer rend = i.next();
			if (rend instanceof AnnotationSupport)
			{
				AnnotationSupport as = (AnnotationSupport)rend;
				boolean updated = as.updateAnnotationFor(m_coordinates, datacoord, pixelcoord, anno);
				if (updated)
				{
					return;
				}
			}
		}		
	}
	public void onMouseUp(Widget sender, int x, int y) {
	}

}
